package com.liy.framework.common.orm.dialect;

public class MySqlDialect extends Dialect
{
    @Override
    public String getQueryDataString(String sql, final int offset, final int limit) {
        sql = sql.trim();
        boolean isForUpdate = false;
        if (sql.toLowerCase().endsWith(" for update")) {
            sql = sql.substring(0, sql.length() - 11);
            isForUpdate = true;
        }
        final StringBuffer sb = new StringBuffer();
        sb.append(" SELECT * FROM  ");
        sb.append(" (").append(sql).append(") __TMP limit ");
        sb.append((offset <= 1) ? 0 : (offset - 1)).append(",").append(limit);
        if (isForUpdate) {
            sb.append(" for update");
        }
        return sb.toString();
    }
    
    @Override
    public String getQueryCountString(final String sql) {
        final StringBuffer buffer = new StringBuffer(sql.length() + 30);
        buffer.append("select count(1) from (").append(sql).append(") AS __TMP ");
        return buffer.toString();
    }
}
